package net.caixiaomi.info.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.sobot.chat.utils.ScreenUtils;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.model.MatchLineupModel;
import net.caixiaomi.info.model.MatchLineupPerson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by suntian on 2018/7/27.
 */

public class MatchLineDrawView extends View{


    private Paint namePaint; //
    private Paint desPaint; //
    private int nameHeight; //裁判的文字高度
    private int desHeight;  //球员的文字高度
    private int closeSize;  //球衣的宽高
    private int FiveRowSpace;   //一行五个球员的间隙
    private int FourRowSpace;   //一行四个球员的间隙

    private int FourLeft;
    private int FiveLeft;

    private float mNameLength;
    private Bitmap homeBitmap ;
    private Bitmap visitorBitmap ;
    private Bitmap GKBitmap ;
    private Context mContext;
    private int mWidth;
    private int mHeight;
    private boolean canDraw=false;
    private MatchLineupModel model;
    private String TEXT="text";
    private int percent=17; //高度17等分

    private int mHomeLines; //主队有几行
    private int mVisitorLines;  //客队有几行

//    private String PositionY[]={"L","CL","C","CR","R"};
    private String PositionY[]={"R","CR","C","CL","L"};
    private String PositionX[]={"GK","D1","D2","DM","M","AM","A"};

//    private HashMap<String,List<MatchLineupPerson>> mHomePersons;   //主队球员
//    private HashMap<String,List<MatchLineupPerson>> mVisitorPersons;   //客队球员

    private List<PersonLine> mHomeLinePerons;
    private List<PersonLine> mVisitorLinePersons;

    private class PersonLine{
        public String PositionX;
        public List<MatchLineupPerson> persons;
        public List<MatchLineupPerson> rowPersons=new ArrayList<>();  //排序后的球员
    }

    public MatchLineDrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        namePaint=new Paint();
        namePaint.setColor(mContext.getResources().getColor(R.color.white));
        namePaint.setStyle(Paint.Style.STROKE);
        namePaint.setTextSize(ScreenUtils.dip2px(mContext,14));
//        namePaint.setTextSize(nameTextSize);
        desPaint=new Paint();
        desPaint.setColor(mContext.getResources().getColor(R.color.white));
        desPaint.setStyle(Paint.Style.STROKE);
//        desPaint.setTextSize(desTextSize);
        desPaint.setTextSize(ScreenUtils.dip2px(mContext,12));
        closeSize=ScreenUtils.dip2px(mContext,30);

         homeBitmap = setImgSize(0,closeSize,closeSize);
         visitorBitmap = setImgSize(1,closeSize,closeSize);
        GKBitmap=setImgSize(2,closeSize,closeSize);
    }

    public void setData(MatchLineupModel model,int width,int height){
        canDraw=true;
        this.model=model;
        mWidth=width;
        mHeight=height;
//        mHomePersons=new HashMap<>();
//        mVisitorPersons=new HashMap<>();

        mHomeLinePerons=new ArrayList<>();
        mVisitorLinePersons=new ArrayList<>();

        FourLeft=ScreenUtils.dip2px(mContext,100)/2;
        FiveLeft=ScreenUtils.dip2px(mContext,70)/2;
        FiveRowSpace=(mWidth-FiveLeft*2)/(5-1);
        FourRowSpace=(mWidth-FourLeft*2)/(4-1);

        if(model.getHlineupPersons()!=null&&model.getHlineupPersons().size()>0){
                for(int j=0;j<PositionX.length;j++){
                    for(int i=0;i<model.getHlineupPersons().size();i++){
                        MatchLineupPerson person=model.getHlineupPersons().get(i);
                    if(person.getPositionX().equals(PositionX[j])) {
                        boolean has = false;
                        for (int m = 0; m < mHomeLinePerons.size(); m++) {
                            if (mHomeLinePerons.get(m).PositionX.equals(PositionX[j])) {
                                has = true;
                                break;
                            }
                        }

                        if (has) {
                            mHomeLinePerons.get(mHomeLinePerons.size() - 1).persons.add(person);
                        } else {
                            PersonLine p = new PersonLine();
                            p.PositionX = PositionX[j];
                            List<MatchLineupPerson> list = new ArrayList<>();
                            list.add(person);
                            p.persons = list;
                            mHomeLinePerons.add(mHomeLinePerons.size(), p);
                        }
                    }

//                    if(person.getPositionX().equals(PositionX[j])){
//                        if(mHomePersons.containsKey(PositionX[j])){
//                            mHomePersons.get(PositionX[j]).add(person);
//                        }else{
//                            List<MatchLineupPerson>list=new ArrayList<>();
//                            list.add(person);
//                            mHomePersons.put(PositionX[j],list);
//                        }
//                    }
                }
            }
            for(int i=0;i<mHomeLinePerons.size();i++){
                List<MatchLineupPerson> lines=mHomeLinePerons.get(i).persons;
                for(int j=0;j<PositionY.length;j++){
                    for(int m=0;m<lines.size();m++){
                        if(lines.get(m).getPositionY().equals(PositionY[j])){
                            mHomeLinePerons.get(i).rowPersons.add(mHomeLinePerons.get(i).rowPersons.size(),lines.get(m));
                        }
                    }
                }
            }
        }


        if(model.getAlineupPersons()!=null&&model.getAlineupPersons().size()>0){
                for(int j=0;j<PositionX.length;j++){
                    for(int i=0;i<model.getAlineupPersons().size();i++){
                        MatchLineupPerson person=model.getAlineupPersons().get(i);
                    if(person.getPositionX().equals(PositionX[j])) {
                        boolean has = false;
                        for (int m = 0; m < mVisitorLinePersons.size(); m++) {
                            if (mVisitorLinePersons.get(m).PositionX.equals(PositionX[j])) {
                                has = true;
                                break;
                            }
                        }

                        if (has) {
                            mVisitorLinePersons.get(mVisitorLinePersons.size() - 1).persons.add(person);
                        } else {
                            PersonLine p = new PersonLine();
                            p.PositionX = PositionX[j];
                            List<MatchLineupPerson> list = new ArrayList<>();
                            list.add(person);
                            p.persons = list;
                            mVisitorLinePersons.add(mVisitorLinePersons.size(), p);
                        }
                    }

//                    if(person.getPositionX().equals(PositionX[j])){
//                        if(mVisitorPersons.containsKey(PositionX[j])){
//                            mVisitorPersons.get(PositionX[j]).add(person);
//                        }else{
//                            List<MatchLineupPerson>list=new ArrayList<>();
//                            list.add(person);
//                            mVisitorPersons.put(PositionX[j],list);
//                        }
//                    }
                }
            }
            for(int i=0;i<mVisitorLinePersons.size();i++){
                List<MatchLineupPerson> lines=mVisitorLinePersons.get(i).persons;
                for(int j=0;j<PositionY.length;j++){
                    for(int m=0;m<lines.size();m++){
                        if(lines.get(m).getPositionY().equals(PositionY[j])){
                            mVisitorLinePersons.get(i).rowPersons.add(mVisitorLinePersons.get(i).rowPersons.size(),lines.get(m));
                        }
                    }
                }
            }
        }

        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        if(canDraw){

            Rect rect = new Rect();
            namePaint.getTextBounds(TEXT, 0, TEXT.length(), rect);
            nameHeight = rect.height();//文本的高度
            Rect rect1 = new Rect();
            desPaint.getTextBounds(TEXT, 0, TEXT.length(), rect1);
            desHeight = rect1.height();//文本的高度

            int lNameSpace=mHeight/(17*2)+nameHeight/2;

            //----画主裁
            canvas.drawText(
                    "主裁"+model.getRefereeName(), ScreenUtils.dip2px(mContext,10),
                    lNameSpace, namePaint);

            //-----画主队阵容
            canvas.drawText(
                    model.getHomeTeamAbbr(), ScreenUtils.dip2px(mContext,20),
                    2*mHeight/17-10, desPaint);
            canvas.drawText(
                    model.getFormationTeamH(), ScreenUtils.dip2px(mContext,20),
                    2*mHeight/17+10+desHeight, desPaint);

            //----画客队阵容
            canvas.drawText(
                    model.getVisitingTeamAbbr(), ScreenUtils.dip2px(mContext,20),
                    15*mHeight/17-10, desPaint);
            canvas.drawText(
                    model.getFormationTeamA(), ScreenUtils.dip2px(mContext,20),
                    15*mHeight/17+10+desHeight, desPaint);



            if(mHomeLinePerons.size()>0) {
                int mHomeHeight = 7 * mHeight / 17-closeSize-ScreenUtils.dip2px(mContext,10);    //----划定主队区域的高度
                int mHomePerHeight = mHomeHeight / (mHomeLinePerons.size()-1);  //主队间隙的高度

                for(int m=0;m<mHomeLinePerons.size();m++){
                    List<MatchLineupPerson> list=mHomeLinePerons.get(m).rowPersons;

                    if(list.size()%2==0){
                        if(list.size()/2==1){
                            for(int i=0;i<list.size();i++) {
                                drawPersonItem(canvas,homeBitmap,FourLeft+FourRowSpace * (i+1) - closeSize / 2,mHeight / 17 + m * mHomePerHeight,desPaint,list.get(i));
                            }
                        }else{
                            for(int i=0;i<list.size();i++) {
                                drawPersonItem(canvas,homeBitmap,FourLeft+FourRowSpace * i - closeSize / 2,mHeight / 17 + m * mHomePerHeight,desPaint,list.get(i));
                            }
                        }
                    }else{
                        {
                            if(list.size()/2==0){
                                for(int i=0;i<list.size();i++) {
                                    drawPersonItem(canvas,homeBitmap,FiveLeft+FiveRowSpace * (i+2) - closeSize / 2,mHeight / 17 + m * mHomePerHeight,desPaint,list.get(i));
                                }
                            }else if(list.size()/2==1){
                                for(int i=0;i<list.size();i++) {
                                    drawPersonItem(canvas,homeBitmap,FiveLeft+FiveRowSpace * (i+1) - closeSize / 2,mHeight / 17 + m * mHomePerHeight,desPaint,list.get(i));
                                }
                            }else{
                                for(int i=0;i<list.size();i++) {
                                    drawPersonItem(canvas,homeBitmap,FiveLeft+FiveRowSpace * i - closeSize / 2,mHeight / 17 + m * mHomePerHeight,desPaint,list.get(i));
                                }
                            }
                        }
                    }
                }
            }


            if(mVisitorLinePersons.size()>0) {
                int mVisitorHeight = 7 * mHeight / 17-closeSize-ScreenUtils.dip2px(mContext,20);    //----划定客队区域的高度
                int mVisitorPerHeight = mVisitorHeight / (mVisitorLinePersons.size()-1);  //客队间隙的高度

                for(int index=1;index<mVisitorLinePersons.size()+1;index++){
                    List<MatchLineupPerson> list=mVisitorLinePersons.get(index-1).rowPersons;
                    if(list.size()%2==0){
                        if(list.size()/2==1){
                            for(int i=0;i<list.size();i++) {
                                drawPersonItem(canvas,visitorBitmap,FourLeft+FourRowSpace * (i+1) - closeSize / 2,mHeight-(mHeight / 17 + index * mVisitorPerHeight),desPaint,list.get(i));
                            }
                        }else{
                            for(int i=0;i<list.size();i++) {
                                drawPersonItem(canvas,visitorBitmap,FourLeft+FourRowSpace * i - closeSize / 2,mHeight-(mHeight / 17 + index * mVisitorPerHeight),desPaint,list.get(i));
                            }
                        }
                    }else{
                        {
                            if(list.size()/2==0){
                                for(int i=0;i<list.size();i++) {
                                    drawPersonItem(canvas,visitorBitmap,FiveLeft+FiveRowSpace * (i+2) - closeSize / 2,mHeight-(mHeight / 17 + index * mVisitorPerHeight),desPaint,list.get(i));
                                }
                            }else if(list.size()/2==1){
                                for(int i=0;i<list.size();i++) {
                                    drawPersonItem(canvas,visitorBitmap,FiveLeft+FiveRowSpace * (i+1) - closeSize / 2,mHeight-(mHeight / 17 + index * mVisitorPerHeight),desPaint,list.get(i));
                                }
                            }else{
                                for(int i=0;i<list.size();i++) {
                                    drawPersonItem(canvas,visitorBitmap,FiveLeft+FiveRowSpace * i - closeSize / 2,mHeight-(mHeight / 17 + index * mVisitorPerHeight),desPaint,list.get(i));
                                }
                            }
                        }
                    }
                }
            }
            canDraw=false;

//        }
    }


    private void drawPersonItem(Canvas canvas, Bitmap bitmap, int left, int top, Paint paint, MatchLineupPerson person){
        if(person.getPositionX().equals("GK")){
            canvas.drawBitmap(GKBitmap, left, top, paint);
        }else{
            canvas.drawBitmap(bitmap, left, top, paint);
        }

        String personName=person.getPersonName();
        if(personName.length()>6){
            personName=personName.substring(0,6);
            personName=personName+"...";
        }
        mNameLength = paint.measureText(personName);
        canvas.drawText(personName,left+closeSize/2-mNameLength/2,top+desHeight+closeSize+ScreenUtils.dip2px(mContext,5),paint);
        mNameLength=paint.measureText(person.getShirtNumber());
        canvas.drawText(person.getShirtNumber(),left+closeSize/2-mNameLength/2,top-(closeSize-desHeight)/2+closeSize,paint);
    }

    public Bitmap setImgSize(int type, int newWidth ,int newHeight) {
        // 获得图片的宽高.
        Bitmap bm=null;

        switch (type){
            case 0:
                bm=BitmapFactory.decodeResource(getResources(),R.drawable.home_team_close);
                break;
            case 1:
                bm=BitmapFactory.decodeResource(getResources(),R.drawable.visitor_team_close);
                break;
            case 2:
                bm=BitmapFactory.decodeResource(getResources(),R.drawable.match_person_keeper);
                break;
        }

        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例.
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数.
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片.
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }


}
